/***************************************************************************
 *   Copyleft 2008-2009 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#include "./vInputSystem.h"

namespace Vendetta {
	vInputSystem::~vInputSystem() {
		mMouseList.Clear();
		mKeybordList.Clear();
	}

	vVoid vInputSystem::AddMouseListener(vMouseListener* list) {
		if (list) {
			for (vUInt uiIndex = 0; uiIndex < mMouseList.Size(); uiIndex++) {
				if (mMouseList[uiIndex] == list) {
					return;
				}
			}
			mMouseList.AddAsLast(list);
		}
	}

	vMouseListener* vInputSystem::RemoveMouseListener(vMouseListener* list) {
		if (list) {
			for (vUInt uiIndex = 0; uiIndex < mMouseList.Size(); uiIndex++) {
				if (mMouseList[uiIndex] == list) {
					vMouseListener* listener = mMouseList[uiIndex];
					mMouseList.Remove(uiIndex);
					return listener;
				}
			}
		}

		return vNull;
	}

	vVoid vInputSystem::AddKeybordListener(vKeyboardListener* list) {
		if (list) {
			for (vUInt uiIndex = 0; uiIndex < mKeybordList.Size(); uiIndex++) {
				if (mKeybordList[uiIndex] == list) {
					return;
				}
			}
			mKeybordList.AddAsLast(list);
		}
	}

	vKeyboardListener* vInputSystem::RemoveKeybordListener(vKeyboardListener* list) {
		if (list) {
			for (vUInt uiIndex = 0; uiIndex < mKeybordList.Size(); uiIndex++) {
				if (mKeybordList[uiIndex] == list) {
					vKeyboardListener* listener = mKeybordList[uiIndex];
					mKeybordList.Remove(uiIndex);
					return listener;;
				}
			}
		}

		return vNull;
	}

	vVoid vInputSystem::FireKeyboardPressedEvent(vUShort cmd) {
		vKeyboardListener::KeyboardEvent ev;
		ev.mVirtualKeyCode = cmd;

		for (vUInt uiIndex = 0; uiIndex < mKeybordList.Size(); uiIndex++) {
			mKeybordList[uiIndex]->KeyPressed(ev);
		}
	}

	vVoid vInputSystem::FireKeyboardReleasedEvent(vUShort cmd) {
		vKeyboardListener::KeyboardEvent ev;
		ev.mVirtualKeyCode = cmd;

		for (vUInt uiIndex = 0; uiIndex < mKeybordList.Size(); uiIndex++) {
			mKeybordList[uiIndex]->KeyReleased(ev);
		}
	}

	vVoid vInputSystem::FireMouseMovedEvent(const vPoint& q) {
		vMouseListener::MouseEvent ev;
		ev.mPos = q;
		ev.mButton = mMouseBtnDown;
		ev.mDeltaPos.x = q.x - mPrevPos.x;
		ev.mDeltaPos.y = q.y - mPrevPos.y;
		mPrevPos = q;

		for (vUInt uiIndex = 0; uiIndex < mMouseList.Size(); uiIndex++) {
			mMouseList[uiIndex]->MouseMoved(ev);
		}
	}

	vVoid vInputSystem::FireMousePressedEvent(const vPoint& q, vMouseListener::eMouseButton eButton) {
		mMouseBtnDown = eButton;
		vMouseListener::MouseEvent ev;
		ev.mPos = q;
		ev.mButton = mMouseBtnDown;

		for (vUInt uiIndex = 0; uiIndex < mMouseList.Size(); uiIndex++) {
			mMouseList[uiIndex]->MousePressed(ev);
		}
	}

	vVoid vInputSystem::FireMouseReleasedEvent(const vPoint& q, vMouseListener::eMouseButton eButton) {
		mMouseBtnDown = eButton;
		vMouseListener::MouseEvent ev;
		ev.mPos = q;
		ev.mButton = mMouseBtnDown;

		for (vUInt uiIndex = 0; uiIndex < mMouseList.Size(); uiIndex++) {
			mMouseList[uiIndex]->MouseReleased(ev);
		}
	}

	vVoid vInputSystem::EnableCapture(vBool bEnable) {
		mCaptureEnabled = bEnable;
	}

	vBool vInputSystem::IsCaptureEnabled() const {
		return mCaptureEnabled;
	}

	vInputSystem::vInputSystem() {
		mCaptureEnabled = true;
		mPrevPos.x = 0;
		mPrevPos.y = 0;
	}

	vInputSystem::vInputSystem(const vInputSystem& input) {
		//TODO ERORR SHOULD BE
	}

	vInputSystem& vInputSystem::operator=(const vInputSystem& input) {
		//TODO ERORR SHOULD BE

		return *this;
	}
}